--
-- Author:      feilong
-- DateTime:    2019-11-22 15:57:48
-- Description: 上庄下庄的管理

-- local Timer = require("Timer")
-- local skynet = require("skynet")
local error_code = require("error_code")
local constant = require("constant")
local UserInfo = import("UserInfo")

local math_random = math.random
local math_abs = math.abs
local math_floor = math.floor
local table_copy = table.copy
local table_insert = table.insert 
local table_remove = table.remove 
local string_split = string.split
local table_sort = table.sort




local BankerManager = class("BankerManager")

function BankerManager:ctor(message_dispatch)
	self.message_dispatch = message_dispatch
	self.banker_list = {} --上庄列表 
    self.unbanker = false --下庄       
    self.banker = nil --当前庄家
    self.sys_banker = nil --系统庄家
    self.can_do_banker_num = 0 --可上庄机器人数
end

function BankerManager:start()
	self.user_manager = global.user_manager
	self.room_config = global.room_config
	self.rule = global.rule
	self.desk = global.desk
    self.table_type = self.room_config:getTableType()

	self.room_data = self.room_config:getRoomData()
end


--
function BankerManager:getBanker()
    return self.banker
end

--取上庄列表
function BankerManager:getBankerList()
    return self.banker_list
end

function BankerManager:getUnbanker()
    return self.unbanker
end

--是否是当前庄家
function BankerManager:isBanker(user)
    if not user or not self.banker then 
        return false
    end
    if self.banker:compare(user) then 
        return true
    end
    return false
end

--uid是否在上庄列表里
function BankerManager:isInBankerList(uid)    
    for k, v in pairs(self.banker_list) do 
        if v == uid then 
            return true
        end
    end
    return false
end

--是否是在下庄列表里
function BankerManager:isUnbanker(user)
    if not self.unbanker then 
        return false
    end
    if self.banker:compare(user) then 
        return true
    end
    return false
end

--设置系统坐庄
function BankerManager:setSystemBanker()    
	if self.sys_banker then
		if self.sys_banker:compare(self.banker) then 
			return true
		end 
		self.banker = self.sys_banker
        if self.banker:getCoins() < 1000000000 then --系统庄家钱太少
            self.banker:setCoins(10000000000)
        end
		return true		
	end
    local base_data = {
        uid = 1,
        coins = 10000000000,           
        vip_level = 0,          
    }
    local enter_data = {
        name = "系统庄家",
        icon = nil,
        sex = nil,
        icon_border = nil, --头像框
    }
    local user = UserInfo.new(base_data, enter_data)
    user:setFD(-1)
    user:setIsRobot(true) --是机器人    
    user:setRobotTypeId(0) --类型id 
    self.banker = user
    self.sys_banker = user
end

--设置系统坐庄
function BankerManager:addBanker(uid)  
    --已在下庄列表     
    table_insert(self.banker_list, uid)
end

function BankerManager:setUnbanker(unbanker)  
    self.unbanker = unbanker
end

function BankerManager:removeBanker(uid)
    for i=#self.banker_list, 1, -1 do 
        if self.banker_list[i] == uid then 
            table_remove(self.banker_list, i)
            return true
        end
    end
    return false
end

--清上庄列表
function BankerManager:clearBankerList()
    local room_data = self.room_data
    local min_banker_coins = room_data.min_banker_coins or 1500000    
    for i=#self.banker_list, 1, -1 do
        local uid = self.banker_list[i]        
        local user = self.user_manager:getUser(uid)
        if not user or user:getDisconnect() or user:getCoins() < min_banker_coins then
            table_remove(self.banker_list, i)
            -- self.desk:sendToUser(uid, "table.RSP_UNDO_BANKER", {result = error_code.BE_TICKED_BANKER})        
        end
    end
end

--检查是否可以继续当庄
function BankerManager:checkBanker()
    local room_data = self.room_data
    local min_banker_coins = room_data.min_banker_coins or 1500000

    if self.unbanker then 
        self.unbanker = false
        self.banker = nil
    end

    self:clearBankerList()
    if self.banker then 
        local user = self.banker
        local uid = user:getUid()        
        if user:getDisconnect() or user:getCoins() < min_banker_coins then
            self.banker = nil
            self.desk:sendToUser(uid, "table.RSP_UNDO_BANKER", {result = error_code.BE_TICKED_BANKER})
        end    
    end
end

--1先找上庄列表的当庄
--2没开放机器人的时候系统当庄
function BankerManager:selectBanker()
	self:checkBanker() --
	self:setCanDoBankerNum()
    if self.banker and self.banker:getUid() > 1 then 
        return 
    end
    while #self.banker_list > 0 do 
        local uid = table_remove(self.banker_list, 1)
        local user = self.user_manager:getUser(uid)
        self.banker = user
        --通知玩家站起
        local seat_id = user:getSeatId()
        if seat_id > 0 then 
		    self.user_manager:standUp(uid)
		    self.desk:sendToAllUser('table.NTF_STANDUP', {uid=uid, seat_id=seat_id})
		end
        self:sendBankerToFriendServer()
        return true
    end
    --系统上庄
    self:setSystemBanker()
    self:sendBankerToFriendServer()
    return true
end

--设置可上庄机器人数
function BankerManager:setCanDoBankerNum()
	--robot_banker_control.lua
	--由在线真人数决定机器人可上庄数
	local user_num = self.user_manager:getRealUserNum()
	local banker_control = self.room_config:getBankerControl()     
	local conf = {
        min = 1,
        max = 10,
        rate = 0,
        number = 0,
    }
    local banker_rates = banker_control.banker_rate
    if not banker_rates then 
        banker_rates = {}
    end
	for k, v in pairs(banker_rates) do 
		if user_num >= v.min and user_num <= v.max then 
			conf = v 
			break
		end
	end    
    local rate = conf.rate
    if math_random(1,100) > rate then
        return
    end
    self.can_do_banker_num = conf.number

end

--取上庄人数配置
function BankerManager:getCanDoBankerNum()
	return self.can_do_banker_num 
end

--判断机器人上庄是符合条件
function BankerManager:canRobotDoBanker(robot)
	--人数
	print("____self.can_do_banker_num___", self.can_do_banker_num)
	if #self.banker_list >= self.can_do_banker_num then 
		return false
	end
    --身上金币是否满足上庄条件
    local room_data = self.room_config:getRoomData()
    local min_banker_coins = room_data.min_banker_coins
    if min_banker_coins > robot:getCoins() then
        dbglog("____canRobotDoBander_____金币不足_____",robot:getUid(), min_banker_coins, robot:getCoins())             
        return false
    end   	
	return true
end

--取机器人可在庄局数
function BankerManager:maxDoBankerCount(coins)
	local banker_control = self.room_config:getBankerControl()
    local obj = banker_control.banker_round[1]
    for _,round_obj in pairs(banker_control.banker_round) do
        if round_obj.min <= coins and round_obj.max >= coins then
            obj = round_obj
            break
        end
    end	
	local range = string_split(obj.round_range,'-')
	local round = math_random(tonumber(range[1]), tonumber(range[2]))
	dbglog("maxDoBankerCount round:",round)	  
	return round
end

--把当前庄发送到好友服(实时游戏数据)
function BankerManager:sendBankerToFriendServer()           
    if not self.banker then 
        return 
    end 
    local is_robot = 0
    if self.banker:isRobot() then 
        is_robot = 1
    end
    local data = {  
                    table_type = self.table_type, 
                    banker_id = self.banker:getUid(),
                    is_robot = is_robot,
                }
    dbglog("sendBankerToFriendServer:", data)   
    R().friendsvr(1):send('.info_mgr', 'set_room_banker', self.table_type, data)
end 






return BankerManager